package com.leetcode.stack_queue;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @author Dennis Li
 * @date 2020/7/21 16:35
 */
public class ImplementStackUsingQueues_225 {

    Queue<Integer> queue;

    /**
     * Initialize your data structure here.
     */
    public void MyStack() {
        queue = new LinkedList<>();
    }

    /**
     * Push element x onto stack.
     */
    public void push(int x) {
        // 先入队，再逐个后移
        queue.offer(x);
        int size = queue.size();
        // 将尾部元素添加到队首，移的次数是n - 1次
        while (size-- > 1) {
            queue.offer(queue.poll());
        }
    }

    /**
     * Removes the element on top of the stack and returns that element.
     */
    public int pop() {
        return queue.poll();
    }

    /**
     * Get the top element.
     */
    public int top() {
        return queue.peek();
    }

    /**
     * Returns whether the stack is empty.
     */
    public boolean empty() {
        return queue.size() == 0;
    }

}
